// pages/userList/index.js
const api = require('../../utils/api')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        roles:wx.getStorageSync('roles'),
        perm:wx.getStorageSync('perm'),
        businesslist:[],
        orderadminlist:[],
        array: [{id:0,name:'美国'},{id:1,name:'中国'}],
        index:0,
        index2:"",
        show:false,
        marketList:[],
        carList:[],
        marketUserList:[],
        market:"",
        marketUser:"",
        marketid:"",
        marketUserId:"",
        name:"",
        orderObj:{},
        page:1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getList()
        this.getMarketList()
        this.getCarList()
    },
    handlAddMessage(){
        // code
        var that = this
        // 判断是否订阅
        
        // 订阅消息
        wx.requestSubscribeMessage({
            tmplIds: ['AHGPUpqZAAT86uafQeSLAhMiQPWNSWhjGped2V5Kh14'],
            success (res) {
                wx.login({
                    success:function(res){
                        const code = res.code
                        api.getopendid({
                          data: {
                        code:code,
                        },
                          method:'post',
                          success: (res)=>{
                              console.log(res,'openid')
                              wx.setStorageSync('openid', res.data.data.data.openid)
                              that.sendMessage(res.data.data.data.openid)
                          }
                      })
                    }
                  })
             },
             fail(err){
                console.log(err);
                request.prompt.showToast('请开启订阅消息权限', 'none')
             }
          })
    },
    sendMessage(openid){
    wx.request({
        url: "https://www.doudouxny.com/doudou/sendsubscribe",
        method: "POST",
        data: {
            openid:openid
        },
        success: res => {
            console.log(res,'发送消息');
        }
    });
 
  
    },
    handleDel(e){

            var id = e.currentTarget.dataset['id'];
            wx.showModal({
             title: '提示',
             content: '确认要删除该车辆?',
             success: function (res) {
                
                let obj = {
                    opcode:"Int16Array",
                    id:id
                }
              if (res.confirm) {
                api.productdel({
                    data:obj,
                    method:'post',
                    success: (res)=>{
                        this.getCarList()
                    }
                })
              } else if (res.cancel) {
                    wx.showToast({
                      title: '取消删除',
                    })
              }
             }
            })
           
    },
    handleEdit(e){
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/operation/index?id='+id+'&type=edit',
          })
    },
    getCarList(){
        let obj = {
            opcode: "Int16Array",
            page: this.data.page,
            pagesize: 1000,
        }
        api.getCarList({
            data:obj,
            method:'post',
            success: (res)=>{
                this.setData({
                    carList:res.data.data.data
                })
            }
        })
    },
    handleSubmit(){
        let obj = {
            data:{
                marketid:this.data.orderObj.marketid,
                name:this.data.name,
                orderid:this.data.orderObj.orderid,
                productid:this.data.orderObj.productid,
                userid:this.data.orderObj.userid,
            },
            opcode:'xxx',
        }
        api.businessadd({
            data:obj,
            method:'post',
            success: (res)=>{
                this.getList()
                this.setData({
                    orderObj:{
                    }
                })
            }
        })
        this.setData({
            show:false
        })
    },
    handleBusiness(e){
        let obj = {
            id:e.currentTarget.dataset.id,
            status:2
        }
        api.businesschangestatus({
            data:obj,
            method:'post',
            success: (res)=>{
                this.getList()
            }
        })
        
    },
    onClickShow(e) {
        console.log(e)
        this.setData({ show: true ,orderObj:{
            orderid:e.currentTarget.dataset.orderid,
            productid:e.currentTarget.dataset.productid,
            userid:e.currentTarget.dataset.userid,
            marketid:e.currentTarget.dataset.marketid
        }
        });
      },
    
    onClickHide() {
        this.setData({ show: false });
    },
    handleOperation() {
        wx.navigateTo({
          url: '/pages/operation/index?type=add',
        })
    },
    handlePerson(){
        wx.navigateTo({
            url: '/pages/person/index?uid='+wx.getStorageSync('uid'),
        })
    },
    bindPickerChange(e){
        this.setData({
            value: parseInt(e.detail.value),
            market:this.data.marketList[parseInt(e.detail.value)].name,
            marketid:this.data.marketList[parseInt(e.detail.value)].id
        })
        this.getMarketUserList(this.data.marketid)
    },
    bindPickerChange2(e){
        this.setData({
            value2: parseInt(e.detail.value),
            marketUser:this.data.marketUserList[parseInt(e.detail.value)].name,
            marketUserId:this.data.marketUserList[parseInt(e.detail.value)].id
        })
        this.getMarketUserList(this.data.marketid)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    getMarketUserList(id){
        let obj = {
            marketid:id,
            opcode:'xxx',
            page:1,
            pagesize:20
        }
        api.marketuserlist({
            data:obj,
            method:'post',
            success: (res)=>{
                this.setData({
                    marketUserList:res.data.data.data
                })
            }
        })
    },
    getMarketList(){
        let obj = {
            opcode:'xxx',
            page:1,
            pagesize:20
        }
        api.marketlist({
            data:obj,
            method:'post',
            success: (res)=>{
                this.setData({
                    marketList:res.data.data.data
                })
            }
        })
    },
    getList(){
        let obj = {
            opcode:'xxx',
            page:1,
            pagesize:20
        }
            api.businesslist({
                data:obj,
                method:'post',
                success: (res)=>{
                    if(!res.data){
                        wx.removeStorageSync("token");
                        wx.navigateTo({
                          url: '../../pages/login2/index',
                        })
                    }
                    this.setData({
                        businesslist:res.data.data.data
                    })
                }
            })
            api.orderadminlist({
                data:obj,
                method:'post',
                success: (res)=>{
                    this.setData({
                        orderadminlist:res.data.data.data
                    })
                }
            })

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getCarList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})